<?php
namespace Operationtest\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\EventManager\EventManagerInterface;
use Zend\View\Model\ViewModel;
use Operationtest\Classe\OperationDate;
use Operationtest\Classe\Table;

class CronController extends AbstractActionController {
    public function setEventManager(EventManagerInterface $events) {
        parent::setEventManager($events);
        
        $controller = $this;
        $events->attach('dispatch', function($e) use($controller) {
            $controller->layout()->setTemplate('layout/operationtest');
        }, 100);
    }
    public function indexAction() {
        $operationDate = new OperationDate();
        $table = new Table($this->getServiceLocator());
        $operationManager = $table->getOperationManager();
        $rhdataTable = $table->getRhdataTable();        
        $stores = $table->getStoreTable()->fetchAll();
        $period = $operationDate->getCustomPeriod('2014-01-26');
        
        $sites = array();
        foreach($stores as $store) {
            $sites[$store->idStore] = $store->groupmentCode;
        }
        
        foreach($period as $day) { 
            foreach($sites as $idStore => $idSite) {
                $idSite = ($idSite==999)?0:$idSite;
                $rhData = $operationManager->getMO($idSite, $day);
                $rhdataTable->saveRhdata($idStore, $day, $rhData);
                var_dump($rhData);
            }
        }
        return new ViewModel();
    }
}

